package com.cy.web;

import com.alibaba.fastjson.JSONArray;
import com.cy.bean.Coach;
import com.cy.bean.Driverschool;
import com.cy.service.CoachService;
import com.cy.token.JwtUtil;
import com.cy.token.ResultResponse;
import com.cy.util.CacheRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class CoachController {
    @Autowired
    private CoachService coachService;
    @Autowired
    private CacheRedisService cacheRedisService;

    @GetMapping("/getCoachList")
    public ResultResponse getCoachList(Coach coach){

        int did= Integer.parseInt(cacheRedisService.get("did"));
        coach.setDid(did);

        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setCode(20000);
        resultResponse.setMessage(JwtUtil.TOKEN_SUCCESS);
        resultResponse.setData(coachService.coachList(coach));
        return resultResponse;
    }

    public ResultResponse getRes(Object object){
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setCode(20000);
        resultResponse.setMessage(JwtUtil.TOKEN_SUCCESS);
        resultResponse.setData(object);
        return resultResponse;
    }

    @GetMapping("/updateState")
    public ResultResponse updateState(Coach coach){

        int did= Integer.parseInt(cacheRedisService.get("did"));

        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setCode(20000);
        resultResponse.setMessage(JwtUtil.TOKEN_SUCCESS);

        if (coachService.updateCoachState(coach)>0){
            Coach coach1 = new Coach();
            coach1.setDid(did);
            resultResponse.setData(coachService.coachList(coach1));
        }

        return resultResponse;
    }

    @GetMapping("/deleteCoach")
    public ResultResponse deleteCoach(Coach coach){

        int did= Integer.parseInt(cacheRedisService.get("did"));

        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setCode(20000);
        resultResponse.setMessage(JwtUtil.TOKEN_SUCCESS);

        if (coachService.deleteCoach(coach)>0){
            Coach coach1 = new Coach();
            coach1.setDid(did);
            resultResponse.setData(coachService.coachList(coach1));
        }
        return resultResponse;
    }

    @PostMapping("/updateCoach")
    public ResultResponse updateCoach(@RequestBody Coach coach){

        int did= Integer.parseInt(cacheRedisService.get("did"));

        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setCode(20000);
        resultResponse.setMessage(JwtUtil.TOKEN_SUCCESS);

        if (coachService.updateCoach(coach)>0){
            Coach coach1 = new Coach();
            coach1.setDid(did);
            resultResponse.setData(coachService.coachList(coach1));
        }

        return resultResponse;
    }

    @PostMapping("/addCoach")
    public ResultResponse addCoach(@RequestBody Coach coach){

        int did= Integer.parseInt(cacheRedisService.get("did"));

        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setCode(20000);
        resultResponse.setMessage(JwtUtil.TOKEN_SUCCESS);
        coach.setDid(did);
        if (coachService.addCoach(coach)>0){
            Coach coach1 = new Coach();
            coach1.setDid(did);
            resultResponse.setData(coachService.coachList(coach1));
        }
        return resultResponse;
    }
}
